<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/4/26
 * Time: 11:33
 */
namespace app\admin\controller;
use think\Db;
class Statistics extends Base {
    public function index(){

        $startHour = date('Y-m-01 00:00:01',time());
        $endHour = date('Y-m-01 23:59:59', strtotime("$startHour +1 hour"));
        return $this->fetch();
    }

    public function broken(){
        $id = input('param.id');
        $startDay = date('Y-m-01',time());
        $endDay = date('Y-m-d', strtotime("$startDay +1 month -1 day"));
        $startHour = date('Y-m-d 00:00:01',time());
        $endHour = date('Y-m-d 23:59:59', strtotime("$startHour +1 hour"));
//        $info = $this->prDates($startDay,$endDay);

//        $tu_status ='1,2,4';
        $res = array();
        $res1 = array();
        $res2 = array();
        $res3 = array();
        $res4 = array();
        $res5 = array();
        if($id==1){//天
            $info = $this->prDates($startDay,$endDay);
            foreach ($info as $key=>$vo){
                //下载
                $where['tu_type'] = 1;
                $where['tu_date'] = $vo;
                $where['tu_status'] = 2;
                $where['tu_task_active'] = ['in','1,3'];
                $count = Db::table('up_task_user')->where($where)->count();
                //求赞求粉
                $where1['tu_type'] = 2;
                $where1['tu_date'] = $vo;
                $where1['tu_status'] = 2;
                $where1['tu_task_active'] = ['in','1,3'];
                $count1 = Db::table('up_task_user')->where($where1)->count();
                //问卷
                $where2['tu_type'] = 3;
                $where2['tu_date'] = $vo;
                $where2['tu_status'] = 2;
                $where2['tu_task_active'] = ['in','1,3'];
                $count2 = Db::table('up_task_user')->where($where2)->count();
                //投票
                $where3['tu_type'] = 4;
                $where3['tu_date'] = $vo;
                $where3['tu_status'] = 2;
                $where3['tu_task_active'] = ['in','1,3'];
                $count3 = Db::table('up_task_user')->where($where3)->count();
                //视频
                $where4['tu_type'] = 5;
                $where4['tu_date'] = $vo;
                $where4['tu_status'] = 2;
                $where4['tu_task_active'] = ['in','1,3'];
                $count4 = Db::table('up_task_user')->where($where4)->count();
                //分享
                $where5['tu_type'] = 6;
                $where5['tu_date'] = $vo;
                $where5['tu_status'] = 2;
                $where5['tu_task_active'] = ['in','1,3'];
                $count5 = Db::table('up_task_user')->where($where5)->count();
                $res[$key] = $count;
                $res1[$key]= $count1;
                $res2[$key] = $count2;
                $res3[$key] = $count3;
                $res4[$key] = $count4;
                $res5[$key] = $count5;
            }
            $result['type_1'] = $res;
            $result['type_2'] = $res1;
            $result['type_3'] = $res2;
            $result['type_4'] = $res3;
            $result['type_5'] = $res4;
            $result['type_6'] = $res5;
            $result['time'] = $info;
        }else{//小时
            $info= $this->prHour($startHour,$endHour);
            foreach ($info as $key=>$vo){
                //下载
                $where['tu_type'] = 1;
                $where['tu_hour'] = $vo;
                $where['tu_status'] = 2;
                $where['tu_task_active'] = ['in','1,3'];
                $count = Db::table('up_task_user')->where($where)->count();
                //求赞求粉
                $where1['tu_type'] = 2;
                $where1['tu_hour'] = $vo;
                $where1['tu_status'] = 2;
                $where1['tu_task_active'] = ['in','1,3'];
                $count1 = Db::table('up_task_user')->where($where1)->count();
                //问卷
                $where2['tu_type'] = 3;
                $where2['tu_hour'] = $vo;
                $where2['tu_status'] = 2;
                $where2['tu_task_active'] = ['in','1,3'];
                $count2 = Db::table('up_task_user')->where($where2)->count();
                //投票
                $where3['tu_type'] = 4;
                $where3['tu_hour'] = $vo;
                $where3['tu_status'] = 2;
                $where3['tu_task_active'] = ['in','1,3'];
                $count3 = Db::table('up_task_user')->where($where3)->count();
                //视频
                $where4['tu_type'] = 5;
                $where4['tu_hour'] = $vo;
                $where4['tu_status'] = 2;
                $where4['tu_task_active'] = ['in','1,3'];
                $count4 = Db::table('up_task_user')->where($where4)->count();
                //分享
                $where5['tu_type'] = 6;
                $where5['tu_hour'] = $vo;
                $where5['tu_status'] = 2;
                $where5['tu_task_active'] = ['in','1,3'];
                $count5 = Db::table('up_task_user')->where($where5)->count();
                $res[$key] = $count;
                $res1[$key]= $count1;
                $res2[$key] = $count2;
                $res3[$key] = $count3;
                $res4[$key] = $count4;
                $res5[$key] = $count5;
            }
            $result['type_1'] = $res;
            $result['type_2'] = $res1;
            $result['type_3'] = $res2;
            $result['type_4'] = $res3;
            $result['type_5'] = $res4;
            $result['type_6'] = $res5;
            $result['time'] = $info;
        }


        return json(['code' => 200, 'msg' => $result]);
    }
    public function prDates($start,$end){
        $arr = array();
        $dt_start = strtotime($start);
        $dt_end = strtotime($end);
        while ($dt_start<=$dt_end){
            $arr[] =  date('Y-m-d',$dt_start);
            $dt_start = strtotime('+1 day',$dt_start);
        }
        return $arr;
    }
    public function prHour($start,$end){
        $arr = array();
        $dt_start = strtotime($start);
        $dt_end = strtotime($end);
        while ($dt_start<=$dt_end){
            $arr[] =  date('Y-m-d H',$dt_start);
            $dt_start = strtotime('+1 hour',$dt_start);
        }
        return $arr;
    }
}